<template>
  <div class="lSelect" @click="openPicker" :style="style">
    <div class="label" v-if="slots_label">
      <span class="require" v-if="props?.rule?.require">*</span>
      <slot name="label" />
    </div>
    <div class="label" v-if="!slots_label"><span class="require" v-if="props?.rule?.require">*</span>{{props.label}}</div>
    <div class="placeholder">
      <div class="value" v-if="props.modelValue">{{getLabel}}</div>
      <div class="text" v-else>{{props.placeholder}}</div>
      <u-icon name="arrow-right" color="#BBBBBB"></u-icon>
    </div>
  </div>
  
  <u-picker :show="show" :columns="[props.columns]" @cancel="closePicker" @confirm="pickerConfirm" keyName="label"></u-picker>
</template>

<script setup>
import { useSlots, ref, computed } from "vue"
const show = ref(false)
const props = defineProps({
  modelValue: '',
  label: '',
  columns: [],
  placeholder: '',
  rule: { require: false, message: '请选择' },
  height: {
    type: Number,
    default: 80
  }
})
const style = {height: props.height + 'rpx'}
const emits = defineEmits(['update:modelValue'])

const getLabel = computed(() => {
  return props.columns.filter(item => item.value == props.modelValue)[0]?.label || ''
})


function openPicker() {
  show.value = true
}
function closePicker() {
  show.value = false
}
function pickerConfirm(val) {
  emits('update:modelValue', val.value[0].value)
  show.value = false
}

const slots_label = !!useSlots().label
</script>

<style lang="scss" scoped>
.lSelect {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.placeholder {
  display: flex;
  align-items: center;
  gap: 8rpx;

  .text {
    color: #bbbbbb;
    font-size: 28rpx;
  }
  .value {
    font-size: 28rpx;
  }
}
.require {
  color: #df5146;
}
.require1 {
  position: absolute;
  left: 42rpx;
}
.label {
  font-size: 28rpx;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: 400;
  text-align: left;
  color: #333333;
  display: flex;
}
</style>